// pages/HomePay/HomePay.js
const app = getApp();
let clearsetInt;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    priceVal: "",
    cursorBol: false,
    isFocus: false,
    phoneValue: wx.getStorageSync('FjLoginLog') ? wx.getStorageSync('FjLoginLog').Mobile : ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenId();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  bindblurInput() {
    let self = this;
    let isFocus = self.data.isFocus;
    if (isFocus) {
      isFocus = false;
      self.setData({
        isFocus,
        cursorBol: isFocus
      })
    }
  },

  /**
   * bindFocus 点击获取焦点
   */
  bindFocus() {
    let self = this;
    let isFocus = self.data.isFocus;
    if (isFocus) {
      return false;
    }
    isFocus = true;
    self.setData({
      isFocus,
      cursorBol: isFocus
    })
  },

  // /**
  //  * focusEvent
  //  * input 获取焦点后触发
  //  */
  // focusEvent(){
  //   let self = this;
  //   let cursorBol = self.data.cursorBol;
  //   cursorBol = true;
  //   self.setData({
  //     cursorBol
  //   })
  // },

  /**
   * blurEvent
   * input 失去焦点后触发
   */
  blurEvent() {
    let self = this;
    let cursorBol = self.data.cursorBol;
    if (!cursorBol) {
      return false;
    }
    cursorBol = false;
    self.setData({
      cursorBol,
      isFocus: cursorBol
    })
  },

  /**
   * inputEvent
   * input 输出时触发
   */
  inputEvent(e) {
    let self = this;
    let priceVal = self.data.priceVal;
    let val = e.detail.value;
    if (val) {
      val = val.match(/\d+\.?\d{0,2}/);
      if (Array.isArray(val)) {
        val = val[0]
      } else {
        val = val ? val : ''
      }
    }
    self.setData({
      priceVal: val
    })
    return val;
  },

  gotoPay() {
    let self = this;
    let priceVal = self.data.priceVal;
    let phoneval = self.data.phoneValue
    //手机号
    if (!phoneval) {
      wx.showToast({
        title: '请先输入手机号！',
        icon: "none"
      })
      return false;
    } else if (!self.requirePhone()) {
      return false;
    } else if // 是否输入金额
    (!priceVal) {
      wx.showToast({
        title: '请先输入金额！',
        icon: "none"
      })
      return false;
    } else if (isNaN(Number(priceVal)) || Number(priceVal) <= 0) {
      wx.showToast({
        title: '请先输入正确的金额！',
        icon: "none"
      })
      return false;
    }
    //是否有openid

    let openid = wx.getStorageSync('openid');
    if (openid) {
      wx.showLoading()
      //请求 下单
      app.getPayPostN("api/FeijiuApp/WxMini_Pay", {
        openid: openid.openid,
        price: priceVal,
        paytel:phoneval,
      }, function (res) {
        wx.hideLoading();
        if (res.code == 200 && res.data) {
          res.data = JSON.parse(res.data)
          // 支付
          wx.requestPayment({
            timeStamp: res.data.timestamp,
            nonceStr: res.data.noncestr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: payRes => {
              console.log(payRes)
              if (payRes.errMsg.indexOf('ok') != -1) {
                self.setData({
                  priceVal: ''
                })
                wx.showModal({
                  title: "支付成功",
                  content: "您已完成支付，谢谢您对Feijiu网的支持！",
                  cancelText: "取消",
                  confirmText: "联系客服",
                  success: function (conRes) {
                    // 点击确认按钮 拨打电话
                    if (conRes.confirm) {
                      wx.makePhoneCall({
                        phoneNumber: '4008116831'
                      })
                    }
                    // 点击取消按钮 返回首页
                    if (conRes.cancel) {

                    }
                  }
                })
              }
            },
            fail: err => {
              if (err.errMsg.indexOf("cancel") != -1) {
                wx.showToast({
                  title: '支付已取消',
                  icon: "none"
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: res.data.Message || '网络出错，请检查网络或稍后再试',
            icon: "none"
          })
        }
      })
    }
  },

  /**
   * 获取openid
   */
  getOpenId: function () {
    var that = this;
    wx.login({
      success: (res) => {
        if (res.code) {
          var api_input = {
            code: res.code
          }
          app.getDaoPostN('api/FeijiuApp/Feijiu_XCX_GetCode', api_input, function (r) {
            if (r.code == 200) {
              wx.setStorageSync('openid', JSON.parse(r.data));
            } else {
              wx.showToast({
                title: '网络延迟较高，请检测网络后再试。',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputPhone(e) {
    this.setData({
      phoneValue: e.detail.value
    })
  },
  requirePhone() { //校验手机号
    console.log('====')
    let a = false
    const regex = /^1[3456789]\d{9}$/; //使用此正则即可
    if (!regex.test(this.data.phoneValue)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      a = false
    } else {
      a = true
    }
    return a
  },
  blurPhone(e) {
    // console.log(e, '手机号失去焦点')
    if (this.data.phoneValue) {
      this.requirePhone()
      return;
    }
  }
})